#include <windows.h>
#include "api.h"
#include "image.h"
#include "data.h"
#include "colorswnd.h"
#include "colordlg.h"

// Capture the palette from popular characters, to the people that want make Fan Art.
// Publish in the web the palettes, ready-to-use

extern PDATA data;

#define paletteClassName "PALETTEWND"

void paletteWndDrawItem(HDC pdc, RECT *rItem, PCOLORSWND data, PCOLORENTRY color, BOOL isSelect){
	RECT rColor = {1, rItem->top+1, 98, rItem->bottom-1};	
	HBRUSH brushColor = CreateSolidBrush(color->color);
	FillRect(pdc, rItem, isSelect?data->brushSelect:data->brushBack);
	FillRect(pdc, &rColor, brushColor);
	DeleteObject(brushColor);
}

void paletteWndPaint(HWND hwnd, HDC pdc, PCOLORSWND data)
{
	int i;
	PCOLORPAGE page = data->page + data->iPage;
	RECT rItem = {0,0,200,20};
	PCOLORENTRY color = page->color;
	for(i=0;i<NRO_COLORSPAGE;i++){
		paletteWndDrawItem(pdc, &rItem, data, color, i==data->iColor);
		OffsetRect(&rItem, 0, 20);
		color++;
	}
}

void paletteWndLButtonDown(HWND hwnd, LPARAM lParam, PCOLORSWND data){
	int iNew = HIWORD(lParam) / 20;
	OutputDebugString("TODO Assign color");
	if (data->iColor == iNew) {
		return;
	}
	data->iColor = iNew;
	InvalidateRect(hwnd, NULL, FALSE);
}

void paletteWndDoubleClick(HWND hwnd, PCOLORSWND data){
	COLORREF color = 0xFF00FF;
	if (colorDlgSelectColor(hwnd, data->curPage->color + data->iColor)){
		InvalidateRect(hwnd, NULL, FALSE);
		data->isChanged = TRUE;
	}
}

void paletteWndSelectColor(HWND hwnd, PCOLORSWND data, UINT flags){
	COLORREF color = data->curPage->color[data->iColor].color;
	if (flags&MK_SHIFT){
		if (flags&MK_CONTROL){
			::data->colorPaper = color;
		}
		else{
			::data->colorInk = color;
		}
	}else{
		if (flags&MK_CONTROL){
			::data->colorBorder = color;
		}
		else{
			::data->colorPainter = color;
		}
	}
	InvalidateRect(GetParent(hwnd), NULL, FALSE);
}

LRESULT CALLBACK paletteWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	GET_DATA(PCOLORSWND);
	switch(message){
	case WM_LBUTTONDOWN:
		paletteWndLButtonDown(hwnd, lParam, data);
		paletteWndSelectColor(hwnd, data, wParam);
		break;
	case WM_LBUTTONDBLCLK:
		paletteWndDoubleClick(hwnd, data);
		paletteWndSelectColor(hwnd, data, wParam);
		break;
	case WM_PAINT:
		ON_PAINT(paletteWndPaint);
		break;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

HWND paletteWndCreate(HWND hwndParent, int x, int y, int dx, int dy, PCOLORSWND data)
{
	HINSTANCE instance = (HINSTANCE)GetWindowLong(hwndParent, GWL_HINSTANCE);
	
    HWND hwnd;
    hwnd = CreateWindowEx (0,
           paletteClassName,
           "ageo - Minerva",
           WS_VISIBLE | WS_CLIPCHILDREN | WS_CHILD | WS_BORDER,
           x,
           y,
           dx+2,	// To the border
           dy+2,	// To the border
           hwndParent,
           NULL,
           instance,
		   data
           );
    ShowWindow (hwnd, SW_SHOW);
    return hwnd;
}

int paletteWndRegister(HINSTANCE instancia)
{
    WNDCLASSEX wincl;

    wincl.hInstance = instancia;
    wincl.lpszClassName = paletteClassName;
    wincl.lpfnWndProc = paletteWndProc;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof (WNDCLASSEX);

    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);

    if (!RegisterClassEx (&wincl))
        return 0;
    return TRUE;
}